package com.zhanggm.springcloud.student.controller;

import com.zhanggm.common.utils.Md5Util;
import com.zhanggm.springcloud.student.entity.AreaEntiy;
import com.zhanggm.springcloud.student.entity.ClassesEntiy;
import com.zhanggm.springcloud.student.entity.HobbyEntiy;
import com.zhanggm.springcloud.student.entity.StudentEntiy;
import com.zhanggm.springcloud.student.service.StudentServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student/")
public class StudentController {

    @Autowired
    private StudentServie studentServie;

    @GetMapping("list")
    public Page<StudentEntiy> getPageInfo(StudentEntiy studentEntiy,
                                          @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize){
//        Md5Util.convertMD5()
        return studentServie.getPageInfo(studentEntiy,pageNum,pageSize);
    }

    @PostMapping("addStudent")
    public StudentEntiy addStudent(@RequestBody StudentEntiy studentEntiy){
        return studentServie.save(studentEntiy);
    }

    @GetMapping("getById")
    public StudentEntiy getById(Long id){
        StudentEntiy stu = studentServie.getById(id);
        List<Long> hobbyIdList = studentServie.getHobbyIdListByStudentId(stu.getId());
        stu.setHobbyIdList(hobbyIdList);
        return stu;
    }

    @PostMapping("addClasses")
    public ClassesEntiy addClasses(ClassesEntiy classesEntiy){
        return studentServie.addClasses(classesEntiy);
    }

    @GetMapping("getClassesAll")
    public List<ClassesEntiy> getClassesAll(){
        return studentServie.getClassesAll();
    }

    @PostMapping("deleteById")
    public boolean deleteById(@RequestBody StudentEntiy studentEntiy){
        boolean b = studentServie.deleteById(studentEntiy.getId());
        return b;
    }

    @PostMapping("deleteByIds")
    public boolean deleteByIds(@RequestParam("ids") List<Long> ids){
        boolean b = studentServie.deleteByIds(ids);
        return b;
    }

    @PostMapping("deleteByIdsStr")
    public boolean deleteByIds(String ids){
        boolean b = studentServie.deleteByIds(ids);
        return b;
    }

    @GetMapping("getAreaListByPid")
    public List<AreaEntiy> getAreaListByPid(Long pid){
        return studentServie.getAreaListByPid(pid);
    }

    @GetMapping("getAreaTree")
    public List<AreaEntiy> getAreaTree(){
        return studentServie.getAreaTree();
    }

    @GetMapping("getHobbyAll")
    public List<HobbyEntiy> getHobbyAll(){
        return studentServie.getHobbyAll();
    }
}
